<template>
  <div class="index">
    <article-list
      :article="article"
      @loadmore="loadmoreArticle"
    />
  </div>
</template>

<script>
import ArticleList from '~/components/archive/list'

export default {
  name: 'DataArticleList',
  validate({ params }) {
    return new Date(params.date).toString() !== 'Invalid Date'
  },
  components: {
    ArticleList
  },
  computed: {
    article() {
      return this.$store.state.article.list
    },
    defaultParams() {
      return {
        date: this.$route.params.date
      }
    },
    nextPageParams() {
      const { current } = this.article.data.pagination
      return Object.assign({
        page: Number(current) + Number(1)
      }, this.defaultParams)
    }
  },
  fetch({ store, params }) {
    return store.dispatch('article/fetchList', params)
  },
  head() {
    return { title: `${this.defaultParams.date} | Date` }
  },
  methods: {
    loadmoreArticle() {
      this.$store.dispatch('article/fetchList', this.nextPageParams)
    }
  }
}
</script>
